<?php

namespace app\site\service\site;

use app\common\exception\DbDefaultException;
use app\site\model\Site;
use app\common\service\BaseService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class SiteService extends BaseService
{

    public function __construct()
    {
        parent::__construct();
        $this->model = new Site();
    }



    public function getSiteList($where = [], $field = '*', $order = '')
    {
        try {
            $list = $this->model->where($where)->field($field)->order($order)->select();
        } catch (DbException $e) {
            throw new \Exception($e->getMessage());
        }
        return $list;
    }

    /**
     * 获取站点信息
     * @param $where
     * @param $field
     * @return array|mixed
     * @throws \Exception
     */
    public function getSiteInfo($where = [], $field = '*')
    {
        try {
            $info = $this->model->field($field)->where($where)->find();
            $info = $info ? $info->toArray() : [];
            if($info) $info['app_type'] = 'site';
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            throw new DbDefaultException($e->getMessage());
        }
        return $info;
    }



    /**
     * 获取站点信息
     * @param $where
     * @param $field
     * @return array|mixed
     * @throws \Exception
     */
    public function getSiteInfoById($site_id, $field = '*')
    {
        try {
            $info = $this->model->field($field)->where('site_id', $site_id)->find();
            $info = $info ? $info->toArray() : [];
            if($info) $info['app_type'] = 'site';
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            throw new DbDefaultException($e->getMessage());
        }
        return $info;
    }



}